package org.tio.sitexxx.service.service.gl;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.model.main.*;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.cache.CacheUtils;
import org.tio.utils.cache.FirsthandCreater;
import org.tio.utils.cache.ICache;

import java.io.Serializable;
import java.util.*;

public class MemberManageService {
    private static Logger log = LoggerFactory.getLogger(MemberManageService.class);
    public static final MemberManageService me = new MemberManageService();

    /**
     * 没有形象测试过的member
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<Member> memberListNotTest(int pageNumber, int pageSize) {
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_member.memberListNotTest";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        Page<Member> page = Member.dao.paginate(pageNumber, pageSize, sqlPara);
        return page;
    }
    /**
     * 带缓存的memberlist  参加过形象测试并顾问没有回答过的member
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<Record> memberListTest(int pageNumber, int pageSize) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_member.memberListTest";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, pageNumber, pageSize);
        Page<Record> records = Db.use(db).paginate(pageNumber, pageSize, sqlPara);
        return records;
    }

    /**
     * 带缓存的memberlist  参加过形象测试并顾问回答过的member
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<Record> memberListTested(int pageNumber, int pageSize) {
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_member.memberListTested";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, pageNumber, pageSize);
        Page<Record> records = Db.use(db).paginate(pageNumber, pageSize, sqlPara);
        return records;
    }

    /**
     * 废弃  事先未经协商  弄复杂了 没必要
     * 返回用户信息表
     *
     * @param pageNumber pageNumber
     * @param pageSize   pageSize
     * @return List<Member>
     */
    public List<Map<String, Object>> memberListJoinImage(int pageNumber, int pageSize) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_member.memberListJoinImage";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, pageNumber, pageSize);
        List<Record> records = Db.use(db).find(sqlPara.getSql(), pageNumber, pageSize);
        System.out.println("list:" + JSON.toJSONString(records));
        if (records != null) {
            records.forEach((record -> {
                resultList.add(JSON.parseObject(JSON.toJSONString(record.getColumns())));
            }));
        }
        insert(resultList, resultList.size());
        return resultList;
    }

    /**
     * 简单插入排序
     * 形象测试过并顾问回答过的排在前面(tested=0)，形象测试过但没有回答的剧中(tested=1)，两者皆无的居末尾(tested=null)
     * tested字段值含义如上
     *
     * @param list
     * @param length
     */
    public static void insert(List<Map<String, Object>> list, int length) {
        int i, j;
        for (i = 1; i < length; i++) {
            if ((list.get(i - 1).get("tested") == null && list.get(i).get("tested") != null) || (list.get(i - 1).get("tested") != null && (Integer.valueOf(list.get(i - 1).get("tested").toString()) == 1) && (list.get(i).get("tested") != null && (Integer.valueOf(list.get(i).get("tested").toString())) == 0))) {
                Map<String, Object> temp = list.get(i);
                for (j = i - 1; ((j >= 0) && ((list.get(j).get("tested") == null && list.get(i).get("tested") != null) || (list.get(j).get("tested") != null && (Integer.valueOf(list.get(j).get("tested").toString()) == 1) && (list.get(i).get("tested") != null && (Integer.valueOf(list.get(i).get("tested").toString()) == 0))))) || j >= 0; j--) {
                    list.set(j + 1, list.get(j));
                }
                list.set(j + 1, temp);
            }
        }
    }

    public Page<Record> questionList(int id, int pageNumber, int pageSize) {
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.getQuestionListBymemberId";
        Kv kv = Kv.by("memberId", id);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, kv);
        Page<Record> page = Db.paginate(pageNumber, pageSize, sqlPara);
        return page;
    }

    public List<Imagetesting> getMemberInformationById(int id) {
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_imagetesting.imagetestingList";
        Kv kv = Kv.by("id", id);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, kv);
        List<Imagetesting> imagetestings = Imagetesting.dao.find(sqlPara);
        return imagetestings;
    }

    public List<Wardrobe> getWardrobeByMemberId(int id) {
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_wardrobe.getWardrobeByMemberId";
        Kv kv = Kv.by("id", id);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, kv);
        List<Wardrobe> wardrobes = Wardrobe.dao.find(sqlPara);
        return wardrobes;
    }

    /**
     * 根据用户电话号码返回用户信息
     *
     * @param mobile
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> findMemberByMobile(String mobile, int type) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String db = Const.Db.TIO_SITE_MAIN;
        String sql;
        if (type == 1) sql = "gl_member.findMemberByMobile";
        else if (type == 2) sql = "gl_member.findMemberByMobileJoinImage";
        else sql = "gl_member.findMemberByMobileJoinImageTested";
        SqlPara sqlParafirst = Db.use(db).getSqlPara(sql, mobile);
        List<Record> records = Db.use(db).find(sqlParafirst.getSql(), mobile);
        if (!records.isEmpty()) {
            records.forEach(record -> {
                resultList.add(JSON.parseObject(JSON.toJSONString(record.getColumns()), Map.class));
            });
        }
        return resultList;
    }

    /**
     * 根据用户姓名返回用户信息
     *
     * @param username
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> findMemberByUsername(String username, int type) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String db = Const.Db.TIO_SITE_MAIN;
        String sql;
        if (type == 1) sql = "gl_member.findMemberByUsername";
        else if (type == 2) sql = "gl_member.findMemberByUsernameJoinImage";
        else sql = "gl_member.findMemberByUsernameJoinImageTested";
        SqlPara sqlParafirst = Db.use(db).getSqlPara(sql, username);
        List<Record> records = Db.use(db).find(sqlParafirst.getSql(), username);
        if (!records.isEmpty()) {
            for (Record record : records) {
                resultList.add(JSON.parseObject(JSON.toJSONString(record.getColumns()), Map.class));
            }
        }
        return resultList;
    }

    public Map<String, Object> findDesginByQuestionId(int id) {
        Map<String, Object> map = new HashMap<>(2);
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_design.findDesginByQuestionId";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, id);
        Record record = Db.use(db).findFirst(sqlPara.getSql(), id);
        if (record != null) {
            map.put("desgin", record.getColumns());
        }
        return map;
    }

    /**
     * 更据问题id查找回答问题的顾问的名字
     *
     * @param questionId
     * @return
     */
    public Map<String, Object> findname(int questionId) {
        Map<String, Object> result = new HashMap<>();
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_design.findname";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, questionId);
        Record record = Db.use(db).findFirst(sqlPara.getSql(), questionId);
        if (record != null) {
            result = record.getColumns();

        }
        return result;
    }
}
